package com.haitonghop.modules.tamdiem;

import java.net.URLDecoder;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.haitonghop.R;
import com.haitonghop.controls.WarperImageView;
import com.haitonghop.data.TamDiemDetailModuleData;
import com.haitonghop.utils.AppSettingParams;
import com.haitonghop.utils.GlobalData;

public class TamDiemDetailModulesRenderAdapter extends ArrayAdapter<TamDiemDetailModuleData> {

	private ArrayList<TamDiemDetailModuleData> blockList = null;
	public TamDiemDetailModulesRenderAdapter(Context context, int resource, int textViewResourceId, ArrayList<TamDiemDetailModuleData> objects) {
		super(context, resource, textViewResourceId, objects);
		this.setBlockList(objects);
	}
	public boolean truyentranh = false;
	public boolean girl = false;

	public void setNewData(ArrayList<TamDiemDetailModuleData> blockList){
		this.setBlockList(blockList);
		notifyDataSetChanged();
	}

	public void clearData(){
		if(blockList != null){
			blockList.clear();
		}
	}

	public void appendData(ArrayList<TamDiemDetailModuleData> inputNewData){
		if(getBlockList() != null && inputNewData != null){
			final int size = inputNewData.size();
			for(int i = 0 ; i < size ; i++){
				getBlockList().add(inputNewData.get(i));
			}
			notifyDataSetChanged();
		}
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowHeader = null;
		if(truyentranh || girl)
			rowHeader = inflater.inflate(R.layout.truyentranh_render_detail_modules, parent, false);
		else
			rowHeader = inflater.inflate(R.layout.tamdiem_render_detail_modules, parent, false);
		rowHeader.setPadding(2,0,2,2);
		TextView header = (TextView) rowHeader.findViewById(R.id.nameView);
		final WarperImageView imageView = (WarperImageView) rowHeader.findViewById(R.id.avatarView);

		TamDiemDetailModuleData module = this.getBlockList().get(position);
		if(!girl){
			header.setText(module.getTitle());
		} else {
			header.setVisibility(View.GONE);
		}


		String url = module.getAvatar();
		int wh = (int)AppSettingParams.SCREEN_WIDTH / 2;
		url = url.replaceAll("widthHere", String.valueOf(wh));

		String decode = URLDecoder.decode(url);

		GlobalData.mAQ.id(imageView).image(decode, GlobalData.opt_image);
//		GlobalData.mAQ.id(imageView).image(decode, true, true, 400, 0,
//                new BitmapAjaxCallback() {
//            @Override
//            public void callback(String url,
//                    ImageView imageview, Bitmap bm,
//                    AjaxStatus status)
//            {
//                if (bm != null)
//                {
//                	if (imageView != null) {
//						imageView.setImageBitmap(bm);
//						imageView.setVisibility(View.VISIBLE);
//					}
//                }
//            }
//        });

//		mImageLoader.DisplayImageView(false, decode, 0, 0, new IImageCached() {
//
//			public void onImageReturn(Activity act,final Bitmap bitmap) {
//				if (act != null) {
//					act.runOnUiThread(new Runnable() {
//
//						public void run() {
//
//							if (imageView != null) {
//								imageView.setImageBitmap(bitmap);
//								imageView.setVisibility(View.VISIBLE);
//							}
//
//						}
//					});
//				}
//			}
//		});
//		Bitmap bitmap = ImageManager.getInstance().getImage(decode, new IImageCached() {
//
//			public void onImageReturn(final Bitmap bitmap) {
//
//				if (bitmap != null && !bitmap.isRecycled()) {
//					if (ImageManager.currentActivity != null) {
//						ImageManager.currentActivity.runOnUiThread(new Runnable() {
//
//							public void run() {
//								if (bitmap != null && !bitmap.isRecycled())
//									imageView.setImageBitmap(bitmap);
//							}
//						});
//					}
//				}
//			}
//		}, true);
//
//		if (bitmap != null) {
//			imageView.setImageBitmap(bitmap);
//		}

		return rowHeader;
	}

	public ArrayList<TamDiemDetailModuleData> getBlockList() {
		return blockList;
	}

	public void setBlockList(ArrayList<TamDiemDetailModuleData> blockList) {
		this.blockList = blockList;
	}
}
